<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/timg.jpg" />
        <span>电商后台管理</span>
      </div>
      <el-button size="mini" type="default" @click="exit">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="collapseFlag ? '60px' : '200px'">
        <div class="toggle-button" @click="toggleClose">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bff"
          unique-opened
          :collapse='collapseFlag'
          :collapse-transition="false"
          router
          :default-active="activeStatus"
        >
          <el-submenu :index="item.id + '' " v-for="item in menuLists" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <el-menu-item :index=" '/' + subItem.path + ''" v-for="subItem in item.children" :key="subItem.id" @click="saveStatus('/' + subItem.path)">
                 <template slot="title">
                    <i class="el-icon-menu"></i>
                    <span>{{subItem.authName}}</span>
                </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuLists: [],
      iconObj: {
        125: 'iconfont icon-yonghu',
        103: 'iconfont icon-quanxianguanli',
        101: 'iconfont icon-shangpinguanli',
        102: 'iconfont icon-dingdanguanli',
        145: 'iconfont icon-shujutongji'
      },
      collapseFlag: false,
      // 存储点击的状态
      activeStatus: ''
    }
  },
  methods: {
    exit () {
      this.$messageBox.confirm('检测到未保存的内容，是否在离开页面前保存修改？', '确认信息', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确定退出',
        cancelButtonText: '取消'
      })
        .then(() => {
          this.$message({
            type: 'info',
            message: '退出成功'
          })
          //  清空token
          window.sessionStorage.clear()
          //  跳转页面
          this.$router.push('/login')
        })
        .catch(action => {
          this.$message({
            type: 'info',
            message: action === 'cancel'
              ? '放弃保存并离开页面'
              : '停留在当前页面'
          })
        })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    // 请求左边栏的数据
    async getcolumnLists () {
      const { data: res } = await this.$http.get('menus')
      this.menuLists = res.data
      console.log(this.menuLists)
    },
    // 关闭折叠
    toggleClose () {
      this.collapseFlag = !this.collapseFlag
    },
    // 保存点击的状态
    saveStatus (activeStatus) {
      window.sessionStorage.setItem('activeKey', activeStatus)
      this.activeStatus = activeStatus
    }
  },
  created () {
    this.getcolumnLists()
    this.activeStatus = window.sessionStorage.getItem('activeKey')
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100vh;
}
.el-header {
  background-color: #3d3741;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  font-weight: 600;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 50x;
      height: 50px;
      border-radius: 50%;
    }
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .el-menu{
    border-right: none;
    text-align: left;
    .el-submenu{
      margin: 0 20px;
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button{
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.3em;
  cursor: pointer;
}
</style>
